package yicr.untils.gis.Polygon;

import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

/**
 * 该类拟表示一个多边形，由于JDK 本身没有double类型的Polygon,采用包装Path2D.Double的方式实现此类。
 * 拟实现除Path2D以外的方法如下:
 * 1. 输入多边形时，批量输入
 * 2. 能计算面积
 * 3. 能找到重心
 */
public class NslPolygon {

    private String id;

    private Path2D.Double path2D;
    private List<Point2D.Double> pointList=new ArrayList<>();


    public NslPolygon(List<Point2D.Double> pointList){
        this.pointList.addAll(pointList);

        path2D=new Path2D.Double();

        Point2D.Double first=pointList.get(0);

        path2D.moveTo(first.x,first.y);

        for (int i = 1; i < pointList.size(); i++) {
            path2D.lineTo(pointList.get(i).x,pointList.get(i).y);
        }

        path2D.lineTo(first.x,first.y);
        path2D.closePath();

    }

    public Path2D.Double getPath2D(){
        return path2D;
    }

    public double getArea(){
        return NslUtilPolygon.getArea(pointList);
    }

    public Point2D.Double getGravityCenterOfPolygon(){
        return NslUtilPolygon.getGravityCenterOfPolygon(pointList);
    }

    public Path2D.Double getPath2d(){
        return  path2D;
    }

    public List<Point2D.Double> getPointList(){
        return pointList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id){
        this.id=id;
    }

    /**
     * 将一个多边形，分解为多个三角形
     */
    public void demo1(){

    }



    public static void main(String[] args) {
        List<Point2D.Double> pointList=new ArrayList<>();
        Point2D.Double p1=new Point2D.Double(115.732003,32.632742);
        Point2D.Double p2=new Point2D.Double(115.732003,32.6762);
        Point2D.Double p3=new Point2D.Double(115.833621,32.6762);
        Point2D.Double p4=new Point2D.Double(115.833621,32.632742);

        pointList.add(p1);
        pointList.add(p2);
        pointList.add(p3);
        pointList.add(p4);


        /*
            Region  1
                    6
                    115.663194 32.88279
                    115.931462 32.976102
                    116.02611 32.732805
                    115.654484 32.736714
                    115.661162 32.882142
                    115.663194 32.88279
            Pen (1,2,0)
            Brush (2,16777215,16777215)
            Center 115.840297 32.854453
        */
        Point2D.Double po1=new Point2D.Double(116.616127,32.83644);
        Point2D.Double po2=new Point2D.Double(116.896207,32.835885);
        Point2D.Double po3=new Point2D.Double(116.894885,32.66778);
        Point2D.Double po4=new Point2D.Double(116.73635,32.78096);
        Point2D.Double po5=new Point2D.Double(116.663688,32.663897);
        Point2D.Double po6=new Point2D.Double(116.615467,32.820351);

        List<Point2D.Double> poList=new ArrayList<>();
        poList.add(po6);
        poList.add(po5);
        poList.add(po4);
        poList.add(po3);
        poList.add(po2);
        poList.add(po1);



        System.out.println(NslUtilPolygon.getArea(poList));

        System.out.println(NslUtilPolygon.getGravityCenterOfPolygon(pointList));

        System.out.println(NslUtilPolygon.getGravityCenterOfPolygon(pointList));

    }





    /**
     * 获取不规则多边形重心点
     *
     * @param mPoints
     * @return
     */
    /*public static Point2D.Double getCenterOfGravityPoint(List<Point2D.Double> mPoints) {
        double area = 0.0;//多边形面积
        double Gx = 0.0, Gy = 0.0;// 重心的x、y
        for (int i = 1; i <= mPoints.size(); i++) {
            double iLat = mPoints.get(i % mPoints.size()).y;
            double iLng = mPoints.get(i % mPoints.size()).x;
            double nextLat = mPoints.get(i - 1).y;
            double nextLng = mPoints.get(i - 1).x;
            double temp = (iLat * nextLng - iLng * nextLat) / 2.0;
            area += temp;
            Gy += temp * (iLat + nextLat) / 3.0;
            Gx += temp * (iLng + nextLng) / 3.0;
        }
        //System.out.println(area);
        Gx = Math.abs(Gx / area);
        Gy = Math.abs(Gy / area);
        return new Point2D.Double(Gx,Gy);
    }*/
}
